package com.bestcem.xm.om.dao.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.om.dao.OmUserDao;
import com.bestcem.xm.om.dao.mapper.OmUserMapper;
import com.bestcem.xm.om.entity.OmUser;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/27 14:29
 */
@Repository
public class OmUserDaoImpl implements OmUserDao {
    @Resource
    private OmUserMapper omUserMapper;

    @Override
    public OmUser findById(String id) {
        return omUserMapper.findById(id);
    }

    @Override
    public void deleteById(String id) {
        omUserMapper.deleteById(id);
    }

    @Override
    public List<OmUser> listAll(Integer page, Integer size) {
        if (Objects.nonNull(page) && Objects.nonNull(size)) {
            return omUserMapper.listAll((page - 1) * size, size);
        }
        return omUserMapper.listAll(null, null);
    }

    @Override
    public void save(List<OmUser> omUsers) {
        for (OmUser omUser : omUsers) {
            omUser.setCreateTime(LocalDateTime.now());
            omUser.setUpdateTime(LocalDateTime.now());
            omUser.setId(ObjectId.next());
        }
        omUserMapper.insertBatch(omUsers);
    }

    @Override
    public OmUser findByUsername(String username) {
        return omUserMapper.findByUsername(username);
    }

    @Override
    public OmUser findByIdAndStatus(String userId, int status) {
        return omUserMapper.findByIdAndStatus(userId, status);
    }

    @Override
    public List<OmUser> listByIdsAndStatus(List<String> userIds, Integer status) {
        return omUserMapper.listByIdsAndStatus(userIds, status);
    }

    @Override
    public List<OmUser> findByCondition(OmUser omUser) {
        return omUserMapper.findByCondition(omUser);
    }

    @Override
    public Long count(String search, String roleId, Integer status) {
        return omUserMapper.count(search, roleId, status);
    }

    @Override
    public List<OmUser> pageUser(Integer page, Integer rowsPerPage, String search, String roleId, Integer status) {
        return omUserMapper.pageUser((page - 1) * rowsPerPage, rowsPerPage, search, roleId, status);
    }

    @Override
    public List<OmUser> findByUsernameOrPhoneOrEmail(String username, String phone, String email) {
        return omUserMapper.findByUsernameOrPhoneOrEmail(username, phone, email);
    }

    @Override
    public OmUser insertUser(OmUser user) {
        LocalDateTime now = LocalDateTime.now();
        user.setId(ObjectId.next());
        user.setCreateTime(now);
        user.setUpdateTime(now);
        omUserMapper.insertUser(user);
        return user;
    }

    @Override
    public void insertBatch(List<OmUser> omUsers) {
        LocalDateTime now = LocalDateTime.now();
        for (OmUser omUser : omUsers) {
            omUser.setCreateTime(now);
            omUser.setUpdateTime(now);
            omUser.setId(ObjectId.next());
        }
        omUserMapper.insertBatch(omUsers);
    }

    @Override
    public long updateById(OmUser omUser) {
        omUser.setUpdateTime(LocalDateTime.now());
        return omUserMapper.updateById(omUser);
    }

    @Override
    public OmUser findByRoleId(String id) {
        return omUserMapper.findByRoleId(id);
    }

    @Override
    public List<OmUser> listByRoleId(List<String> roleIds) {
        return omUserMapper.listByRoleId(roleIds);
    }

    @Override
    public List<OmUser> findByIds(Collection<String> idCollection) {
        return omUserMapper.listByIds(idCollection);
    }

    /**
     * 根据用户名模糊查询用户列表
     *
     * @param userName
     * @return
     */
    @Override
    public List<OmUser> listByUsername(String userName) {
        return omUserMapper.listByUsername(userName);
    }

    @Override
    public List<OmUser> listByRealNameAndRoleIds(String realNamePattern, List<String> roleIds, Integer offset, Integer size) {
        return omUserMapper.listByRealNameAndRoleIds(realNamePattern, roleIds, offset, size);
    }
}
